package cn.com.mayue.guava.eventBus;

import com.google.common.collect.Lists;
import com.google.common.eventbus.EventBus;
import org.junit.Test;

import java.util.List;

/**
 * Created by mayue on 2017/4/5.
 * 事件总线 测试
 */
public class EventBusTest {
    /**
     * 重复注册监听器测试
     */
    @Test
    public void testRepeatRegister() {
        final EventBus bus = new EventBus();
        bus.register(new OneEventListener());
        bus.register(new OneEventListener());

        OneEvent event = new OneEvent(SystemEvent.ONE_EVENT, "123");
        bus.post(event);
    }


    /**
     * 并发测试
     * @AllowConcurrentEvents 保证现场安装
     */
    @Test
    public void testConcurrent() {
        TwoEvent event = new TwoEvent(SystemEvent.TWO_EVENT, "456");
        for (int i = 0; i < 100; i++) {
            final int cnt = i;
            Thread t = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("第" + cnt + "次");
                    EventBusFactory.build().register(TwoEventListener.class);
                }
            });
            t.start();
        }
        try {
            Thread.sleep(6000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        EventBusFactory.build().postsEvent(event);
    }

    /**
     * 测试继承
     */
    @Test
    public void testExtends() {
        OneEvent event = new OneEvent(SystemEvent.ONE_EVENT, "123");
        EventBusFactory.build().register(OneEventListener.class);
        EventBusFactory.build().postsEvent(event);
    }
}
